﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.MvcPassiveView;
using POSPlus.PrintManager.Core;
using System.ComponentModel;

namespace POSPlus.PrintManager
{
	public class PrintManagerControl : MvcControlBase<PrintManagerModel, IPrintManagerView>
	{
		public PrintManagerControl(PrintManagerModel model, IPrintManagerView view)
		{
			view.PrintAll += new PrintAllButtonEventHandler(view_PrintAll);
			view.PrintSelected += new PrintSelectedButtonEventHandler(view_PrintSelectedButtonClicked);
			view.RemoveButton += new RemoveButtonEventHandler(view_RemoveButton);
			view.EditPrintJob += new EditPrintJobEventHandler(view_EditPrintJob);
			Initialize(model, view);
		}

		protected override void UnwireEvents()
		{
			Model.PropertyChanged -= Model_PropertyChanged;
			View.UnRegisterChangeRequestListener<List<IPrintJob>>("PrintJobs", View_OnPrintJobsChangeRequest);
		}

		protected override void WireEvents()
		{
			Model.PropertyChanged += Model_PropertyChanged;
			View.RegisterChangeRequestListener<List<IPrintJob>>("PrintJobs", View_OnPrintJobsChangeRequest);
		}

		private void View_OnPrintJobsChangeRequest(Object sender, PropertyChangeRequestEventArgs<List<IPrintJob>> args)
		{
			Model.PrintJobs = args.RequestedValue;
		}

		private void Model_PropertyChanged(Object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "PrintJobs":
					View.PrintJobs = Model.PrintJobs;
					break;
				default:
					throw new ArgumentException("Property not handled: " + e.PropertyName);
			}
		}

		protected override void SetViewState()
		{
			View.PrintJobs = Model.PrintJobs;
		}

		void view_RemoveButton(object sender, RemoveEventArgs e)
		{
			Model.RemoveJobs(e.JobNames);
		}

		void view_PrintSelectedButtonClicked(object sender, PrintEventArgs e)
		{
			Model.Print(e.JobNames);
		}

		void view_PrintAll(object sender, PrintEventArgs e)
		{
			Model.Print(e.JobNames);
		}

		void view_EditPrintJob(string jobName)
		{
			Model.JobViewDialog(jobName);
		}
	}
}
